home *** CD-ROM | disk | FTP | other *** search
/ Freelog 117 / FreelogNo117-OctobreNovembre2013.iso / Programmation / jedit / jedit5.1.0install.exe / {app} / macros / Java / Create_Constructor.bsh next >
Text File  |  2013-07-28  |  7KB  |  243 lines

  1. /**
  2. Create_Constructor.bsh - a BeanShell macro for the jEdit text editor  that 
  3. creates a constructor containing the selected variables.  This code has the 
  4. similar limitations as Get_Class_Name; it merely looks backwards for the nearest 
  5. occurance of the keyword 'class," etc.
  6.  
  7. Copyright (C)  2004 Thomas Galvin - software@thomas-galvin.com
  8.  
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or any later version.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18. */
  19.  
  20. //Localization
  21. final static String UnevenNumberMessage = jEdit.getProperty("macro.rs.CreateConstructor.UnevenNumber.message", "Uneven number of type names and variables.");
  22. final static String NotEditableMessage = jEdit.getProperty("macro.rs.general.ErrorNotEditableDialog.message", "Buffer is not editable");
  23.     
  24. // Process
  25. boolean JAVA_MODE = buffer.getMode().getName().toLowerCase().equals("java");
  26. String UNDEFINED = "UNKNOWN_CLASS";
  27.  
  28. void setCaret(int selectionStart, int selectionEnd)
  29. {
  30.   textArea.setCaretPosition(selectionStart);
  31.   textArea.moveCaretPosition(selectionEnd);
  32. }
  33.  
  34. String getClassName()
  35. {
  36.   int selectionStart;
  37.   int selectionEnd;
  38.   if(textArea.getSelection().length != 0){  // if there are selections exists
  39.       selectionStart = textArea.getSelection(0).getStart();
  40.       selectionEnd = textArea.getSelection(0).getEnd();
  41.   }
  42.   else{        // if no selection
  43.       selectionStart = textArea.getCaretPosition();
  44.       selectionEnd = textArea.getCaretPosition();
  45.   }
  46.   
  47.   String text = textArea.getText();
  48.   int index = text.lastIndexOf("class", selectionStart);
  49.   if(index != -1)
  50.   {
  51.     textArea.setCaretPosition(index);
  52.     int lineNumber = textArea.getCaretLine();
  53.     int lineEnd = textArea.getLineEndOffset(lineNumber);
  54.     String lineText = text.substring(index, lineEnd);
  55.     
  56.     StringTokenizer tokenizer = new StringTokenizer(lineText);
  57.     tokenizer.nextToken(); //eat "class"
  58.     if(tokenizer.hasMoreTokens())
  59.     {
  60.       setCaret(selectionStart, selectionEnd);
  61.       return tokenizer.nextToken();
  62.     }
  63.   }
  64.   setCaret(selectionStart, selectionEnd);
  65.   
  66.   String fileClassName = buffer.getName();
  67.   int index = fileClassName.lastIndexOf('.');
  68.   if(index != -1)
  69.   {
  70.     fileClassName = fileClassName.substring(0, index);
  71.     if(fileClassName.toLowerCase().indexOf("untitled") == -1)
  72.     {
  73.       return fileClassName;
  74.     }
  75.   }
  76.   
  77.   return UNDEFINED;
  78. }
  79.  
  80. public String createJavaConstructor(String className, String[] typeNames, String[] variableNames)
  81. {
  82.   if(typeNames.length != variableNames.length)
  83.   {
  84.     Macro.message(view, UnevenNumberMessage);
  85.     return "";
  86.   }
  87.   
  88.   String args = "";
  89.   String body = "";
  90.   
  91.   for(int i = 0; i < typeNames.length; i++)
  92.   {
  93.     args += typeNames[i] + " " + variableNames[i];
  94.     if(i+1 < typeNames.length)
  95.     {
  96.       args += ",\n";
  97.     }
  98.     
  99.     body += "this." + variableNames[i] + " = " + variableNames[i] + ";\n";
  100.   }
  101.   
  102.   String code = 
  103.   "/**\n" + "Basic constructor for " + className + "\n*/\n" + 
  104.   "public " + className + 
  105.   "(" +  args + ")" + "\n" + 
  106.   "{" + "\n" + 
  107.     body + 
  108.   "}" + "\n";
  109.   
  110.   return code;
  111. }
  112.  
  113. public String createCppConstructor(String className, String[] typeNames, String[] variableNames)
  114. {
  115.   if(typeNames.length != variableNames.length)
  116.   {
  117.     Macro.message(view, UnevenNumberMessage);
  118.     return "";
  119.   }
  120.   
  121.   String args = "";
  122.   String body = "";
  123.   
  124.   for(int i = 0; i < typeNames.length; i++)
  125.   {
  126.     String setVariable = variableNames[i] + "Value";
  127.     
  128.     args += typeNames[i] + "& " + setVariable;
  129.     body += variableNames[i] + "(" + setVariable + ")";
  130.     
  131.     if(i+1 < typeNames.length)
  132.     {
  133.       args += ",\n";
  134.       body += ",";
  135.     }
  136.     
  137.     body += "\n";
  138.   }
  139.   
  140.   String code = 
  141.   "/*\n" + "Basic constructor for " + className + "\n*/\n" + 
  142.   className + "::" + className +  
  143.   "(" +  args + ")" + "\n" + 
  144.   "throw()\n" + 
  145.   " : " + body + 
  146.   "{" + "\n" + 
  147.   "}" + "\n";
  148.   
  149.   return code;
  150. }
  151.  
  152. void parseSelection()
  153. {
  154.   int selectionStart;
  155.   int selectionEnd;
  156.   if(textArea.getSelection().length != 0){  // if there are selections exists
  157.       selectionStart = textArea.getSelection(0).getStart();
  158.       selectionEnd = textArea.getSelection(0).getEnd();
  159.   }
  160.   else{        // if no selection
  161.       selectionStart = textArea.getCaretPosition();
  162.       selectionEnd = textArea.getCaretPosition();
  163.   }
  164.   
  165.   textArea.setCaretPosition(selectionStart);
  166.   int startLine = textArea.getCaretLine();
  167.   
  168.   textArea.setCaretPosition(selectionEnd);
  169.   int endLine = textArea.getCaretLine();
  170.   
  171.   Vector typeNames = new Vector();
  172.   Vector variableNames = new Vector();
  173.   
  174.   for(int i = startLine; i <= endLine; i++)
  175.   {
  176.     String lineText = textArea.getLineText(i);
  177.     if( lineText != null && !lineText.equals("") )
  178.     {
  179.       lineText = lineText.trim();
  180.       if( lineText.endsWith(";") )
  181.       {
  182.         lineText = lineText.substring( 0, lineText.length() -1 );
  183.       }
  184.       
  185.       StringTokenizer tokenizer = new StringTokenizer(lineText);
  186.       int tokenCount = tokenizer.countTokens();
  187.       if(tokenCount >= 2)
  188.       {
  189.         int numGarbage = tokenCount - 2;
  190.         for (int i = 0; i < numGarbage; i++)
  191.         {
  192.           tokenizer.nextToken();
  193.         }
  194.         
  195.         String type = tokenizer.nextToken();
  196.         String variable = tokenizer.nextToken();
  197.         
  198.         if(type != null &&
  199.            type.compareTo("") != 0 &&
  200.            variable != null &&
  201.            variable.compareTo("") != 0 )
  202.         {
  203.           typeNames.add(type);
  204.           variableNames.add(variable);
  205.         }
  206.       }
  207.     }
  208.   }
  209.   
  210.   int size = typeNames.size();
  211.   String [] types = new String[size];
  212.   String [] variables = new String[size];
  213.   
  214.   for(int i = 0; i < size; i++)
  215.   {
  216.     types[i] = typeNames.get(i).toString();
  217.     variables[i] = variableNames.get(i).toString();
  218.   }
  219.   
  220.   String code = "\n";
  221.   if(JAVA_MODE)
  222.   {
  223.     code += createJavaConstructor(getClassName(), types, variables);
  224.     textArea.setCaretPosition(selectionEnd);
  225.     textArea.setSelectedText(code);
  226.   }
  227.   else
  228.   {
  229.     code += createCppConstructor(getClassName(), types, variables);
  230.     textArea.setCaretPosition(selectionEnd);
  231.     textArea.setSelectedText(code);
  232.   }
  233.   
  234.   textArea.setCaretPosition(selectionEnd);
  235.   textArea.moveCaretPosition(selectionEnd + code.length(), true);
  236.   textArea.indentSelectedLines();
  237. }
  238.  
  239. if( buffer.isReadOnly() )
  240.     Macros.error( view, NotEditableMessage );
  241. else
  242.     parseSelection();
  243.